package io.renren.modules.diycake.controller;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Map;

import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.diycake.entity.FileEntity;
import io.renren.modules.diycake.service.FileService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author mohan
 * @email 1358665425@qq.com
 * @date 2022-01-09 16:40:26
 */
@RestController
@RequestMapping("diycake/file")
public class FileController {
    @Autowired
    private FileService fileService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("diycake:file:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = fileService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("diycake:file:info")
    public R info(@PathVariable("id") Long id){
		FileEntity file = fileService.getById(id);

        return R.ok().put("file", file);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("diycake:file:save")
    public R save(@RequestBody FileEntity file){
		fileService.save(file);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("diycake:file:update")
    public R update(@RequestBody FileEntity file){
		fileService.updateById(file);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("diycake:file:delete")
    public R delete(@RequestBody Long[] ids){
		fileService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("upload/{cakeId}")
    public R upload(MultipartFile file,@PathVariable Long cakeId) {
        String s = fileService.upload(file,cakeId);
        return R.ok().put("url",s);
    }

    @GetMapping("download/{significantBits}")
    public String download(@PathVariable Long significantBits) {
        String r = null;
        try {
            r = fileService.download(significantBits);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return r;
    }
}
